package ru.urfc.portal.model.radio;

import java.util.Date;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import ru.urfc.portal.ui.client.Constants;

@Table(name = "DocsRes_v")
@Entity
@XmlRootElement(name = Constants.CERTIFICATE)
public class DeviceCertificate implements Comparable<DeviceCertificate> {
	public static final Long STATUS_ACTIVE = 1L;
	
	@Id
	@Column(name = "certificateGUID")
	private String guid;
	@Column(name = "ourResGUID")
	private String deviceGuid;
	private Long status;
	@Column(name = "certificateSeria")
	public String seria;
	@Column(name = "certificateNum")
	public String number;
	@Column(name = "certificateDate")
	@Temporal(TemporalType.DATE)
	public Date issueDate;
	@Column(name = "endDate")
	@Temporal(TemporalType.DATE)
	public Date expireDate;
	@Column(name = "downloaded")
	@Temporal(TemporalType.DATE)
	private Date downloadDate;
	@Column(name = "wdRecId")
	private Long x3;
	
	public DeviceCertificate() {
		super();
	}

	public DeviceCertificate(Long status, DeviceWithExtra device, String seria, String number, Date issueDate, Date expireDate) {
		this.status = status;
		this.guid = UUID.randomUUID().toString();
		this.deviceGuid = device.getGuid();
		this.seria = seria;
		this.number = number;
		this.issueDate = issueDate;
		this.expireDate = expireDate;
	}
	
	public DeviceCertificate(String guid, String deviceGuid, String number,	Date downloadDate, Long x3) {
		this.guid = guid;
		this.deviceGuid = deviceGuid;
		this.number = number;
		this.downloadDate = downloadDate;
		this.x3 = x3;
	}

	public String getGuid() {
		return guid;
	}
	
	public String getDeviceGuid() {
		return deviceGuid;
	}

	@XmlTransient
	public Date getDownloadDate() {
		return downloadDate;
	}
	
	@XmlTransient
	public Long getX3() {
		return x3;
	}
	
	@XmlTransient
	public Long getStatus() {
		return status;
	}
	
	public int compareGuids(DeviceCertificate o) {
		int compareGuids = guid.compareTo(o.guid);
		return compareGuids != 0? compareGuids: deviceGuid.compareTo(o.deviceGuid); 
	}
	
	@Override
	public int compareTo(DeviceCertificate o) {
		int compareGuids = compareGuids(o);
		if (compareGuids != 0) {
			return compareGuids;
		}
		int compareDownloadDates = downloadDate.compareTo(o.downloadDate);
		return compareDownloadDates != 0? compareDownloadDates: x3.compareTo(o.x3); 
	}
	
}